package com.tanhua.server.controller;

import com.tanhua.server.service.QuestionService;
import com.tanhua.server.service.TanHuaService;
import com.tanhua.server.service.TodayBestService;
import com.tanhua.server.vo.NearUserVo;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.RecommendUserQueryParam;
import com.tanhua.server.vo.TodayBest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("tanhua")
public class TodayBestController {
    @Autowired
    private TanHuaService tanHuaService;
    @Autowired
    private TodayBestService todayBestService;
    @Autowired
    private QuestionService questionService;


    /**
     * 实现功能:今日佳人
     */
    @GetMapping("todayBest")
    public TodayBest todayBest() {
        TodayBest todayBest = null;
        try {
            todayBest = todayBestService.todayBest();
            if (todayBest == null) {
                return null;
            }
        } catch (Exception e) {
            log.error("获取今日佳人异常!");
            return null;
        }
        return todayBest;
    }

    /**
     * 控制层实现功能:获取系统推荐的今日佳人
     */
    @GetMapping("recommendation")
    public ResponseEntity<PageResult> recommendation(RecommendUserQueryParam param) {
        PageResult pageResult = null;
        Integer page = param.getPage();
        Integer pageSize = param.getPagesize();
        try {
            pageResult = todayBestService.getAlltTodayBest(page, pageSize, param);
            if (pageResult == null) {
                log.error("获取的佳人信息结果为空!");
                //return ResponseEntity.status(500).build();
                return ResponseEntity.ok(new PageResult());
            }
        } catch (Exception e) {
            log.error("获取的佳人信息结果异常!");
            return ResponseEntity.status(500).build();
        }
        return ResponseEntity.ok(pageResult);
    }


    /**
     * 查询个人主页的个人信息+
     *
     * @param userId
     * @return
     */
    @GetMapping("{id}/personalInfo")
    public ResponseEntity<TodayBest> queryUserInfo(@PathVariable("id") Long userId) {
        try {
            TodayBest todayBest = this.tanHuaService.queryUserInfo(userId);
            return ResponseEntity.ok(todayBest);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 获取陌生人的问题
     *
     * @param userId
     * @return
     */
    @GetMapping("strangerQuestions")
    public ResponseEntity<String> getMoShengRenWenTi(@RequestParam("userId") Integer userId) {
        try {
            String question = questionService.getQuestion(userId);
            if (questionService != null) {
                return ResponseEntity.ok(question);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage().toString());
            return ResponseEntity.status(500).build();
        }
        return ResponseEntity.status(500).build();
    }

    /**
     * 回答:陌生人的问题
     * @param param
     * @return
     */
    @PostMapping("strangerQuestions")
    public ResponseEntity<Void> replyQuestion(@RequestBody Map<String, Object> param) {
        try {
            Long userId = Long.valueOf(param.get("userId").toString());
            String reply = param.get("reply").toString();
            Boolean result = this.questionService.postQuestion(userId, reply);
            if (result) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 搜附近
     *
     * @param gender
     * @param distance
     * @return
     */
    @GetMapping("search")
    public ResponseEntity<List<NearUserVo>> queryNearUser(@RequestParam(value = "gender", required = false) String gender,
                                                          @RequestParam(value = "distance", defaultValue = "2000") String distance) {
        try {
            List<NearUserVo> list = this.tanHuaService.queryNearUser(gender, distance);
            return ResponseEntity.ok(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }





    /**
     * 探花
     *
     * @return
     */
    @GetMapping("cards")
    public ResponseEntity<List<TodayBest>> queryCardsList() {
        try {
            List<TodayBest> list = this.tanHuaService.queryCardsList();
            return ResponseEntity.ok(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 喜欢
     *
     * @param likeUserId
     * @return
     */
    @GetMapping("{id}/love")
    public ResponseEntity<Void> likeUser(@PathVariable("id") Long likeUserId) {
        try {
            this.tanHuaService.likeUser(likeUserId);
            return ResponseEntity.ok(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 不喜欢
     *
     * @param likeUserId
     * @return
     */
    @GetMapping("{id}/unlove")
    public ResponseEntity<Void> notLikeUser(@PathVariable("id") Long likeUserId) {
        try {
            this.tanHuaService.notLikeUser(likeUserId);
            return ResponseEntity.ok(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }



}
